/**
 * city基础接口
 */
const AV = require("leanengine");
const BaseService = require("./baseService");
class Service extends BaseService {
  constructor() {
    super("Cinema", "影院");
  }

  async add(ctx) {
    let params = ctx.params;
    // 属性校验
    let result = this.checkAttr(
      [
        "name",
        "cityId",
        "cityName",
        "areaId",
        "areaName",
        "address",
        "lowPrice"
      ],
      params
    );
    if (result) return result;
    try {
      result = await this.checkName(params.name);
      if (result) return result;
      result = await this.addInterface(params);
      return {
        result,
        code: "666",
        msg: "success"
      };
    } catch (error) {
      return {
        code: error.code,
        msg: error.message
      };
    }
  }

  async edit(ctx) {
    let params = ctx.params;
    // 属性校验
    let result = this.checkAttr(
      [
        "name",
        "cinemaId",
        "cityId",
        "cityName",
        "areaId",
        "areaName",
        "address",
        "lowPrice"
      ],
      params
    );
    if (result) return result;
    try {
      let cinemaId = params.cinemaId;
      const entry = AV.Object.createWithoutData("Cinema", cinemaId);
      for (let key in params) {
        if (key !== "cinemaId") {
          entry.set(key, params[key]);
        }
      }
      result = await entry.save();
      return {
        result,
        code: "666",
        msg: "success"
      };
    } catch (error) {
      return {
        code: error.code,
        msg: error.message
      };
    }
  }

  async getDetail(ctx) {
    let { cinemaId } = ctx.params;
    let result = this.checkAttr(["cinemaId"], ctx.params);
    if (result) return result;
    let query = new AV.Query("Cinema");
    try {
      let result = await query.get(cinemaId);
      let cinema = {
        ...result.attributes,
        cinemaId: result.id
      };
      return {
        cinema,
        code: "666",
        msg: "success"
      };
    } catch (error) {
      let { code, message } = error;
      return {
        code,
        msg: code === 101 ? "影院不存在" : message
      };
    }
  }

  async delById(ctx) {
    const { cinemaId } = ctx.params;
    let result = this.checkAttr(["cinemaId"], ctx.params);
    if (result) return result;
    try {
      const resObj = await this.delInterfaceById(cinemaId);
      return {
        code: "666",
        data: resObj
      };
    } catch (error) {
      return {
        code: 500,
        ...error,
        msg: "删除失败"
      };
    }
  }

  async getList(ctx) {
    let { pageNum = 1, pageSize = 10 } = ctx.params;
    try {
      const query = new AV.Query(this.moduleName);
      const res = await query.find();
      const total = await query.count();
      query.limit(pageSize);
      query.skip(pageSize * pageNum - pageSize);
      query.descending("updatedAt");
      let result = await query.find();
      let cinemas = result.map(cinema => {
        return {
          ...cinema.attributes,
          cinemaId: cinema.id
        };
      });
      return {
        pageNum,
        pageSize,
        total,
        code: "666",
        cinemas
      };
    } catch (error) {
      return {
        code: error.message,
        msg: error.message
      };
    }
  }
}

const service = new Service();
module.exports = service;
